package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsModelRelation;
import net.realsee.domain.wcs.data.bo.WcsModelRelationBo;
import net.realsee.domain.wcs.data.vo.WcsModelRelationVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsModelRelationMapper;
import net.realsee.service.wcs.data.IWcsModelRelationService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 机种关系数据维护各类资源与机种之间的对应关系Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsModelRelationServiceImpl extends ServiceImpl<WcsModelRelationMapper, WcsModelRelation> implements IWcsModelRelationService {

    private final WcsModelRelationMapper baseMapper;

    /**
     * 查询机种关系数据维护各类资源与机种之间的对应关系
     */
    @Override
    public WcsModelRelationVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询机种关系数据维护各类资源与机种之间的对应关系列表
     */
    @Override
    public TableDataInfo<WcsModelRelationVo> selectVoPage(WcsModelRelationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsModelRelation> lqw = buildVoQueryWrapper(bo);
        Page<WcsModelRelationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询机种关系数据维护各类资源与机种之间的对应关系列表
     */
    @Override
    public List<WcsModelRelationVo> selectVoList(WcsModelRelationBo bo) {
        LambdaQueryWrapper<WcsModelRelation> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsModelRelation> buildVoQueryWrapper(WcsModelRelationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsModelRelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsModelRelation::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getResCode()), WcsModelRelation::getResCode, bo.getResCode());
        lqw.eq(StringUtils.isNotBlank(bo.getModelCode()), WcsModelRelation::getModelCode, bo.getModelCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRelationClass()), WcsModelRelation::getRelationClass, bo.getRelationClass());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsModelRelation::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsModelRelation::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsModelRelation::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsModelRelation::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsModelRelation::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsModelRelation::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsModelRelation::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsModelRelation::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsModelRelation::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsModelRelation::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsModelRelation::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsModelRelation::getRowVersionStamp, bo.getRowVersionStamp());
        return lqw;
    }

    /**
     * 新增机种关系数据维护各类资源与机种之间的对应关系
     */
    @Override
    public Boolean insertByBo(WcsModelRelationBo bo) {
        WcsModelRelation add = BeanUtil.toBean(bo, WcsModelRelation.class);
        return this.save(add);
    }

    /**
     * 修改机种关系数据维护各类资源与机种之间的对应关系
     */
    @Override
    public Boolean updateByBo(WcsModelRelationBo bo) {
        WcsModelRelation update = BeanUtil.toBean(bo, WcsModelRelation.class);
        return this.updateById(update);
    }

    /**
     * 批量删除机种关系数据维护各类资源与机种之间的对应关系
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
